// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.standard.service.impl;

import com.liuxinlong.common.AosException;
import com.liuxinlong.common.TreeNode;
import com.liuxinlong.enums.ReturnCodes;
import com.liuxinlong.modules.dao.ClassificationDao;
import com.liuxinlong.modules.dao.StandardDao;
import com.liuxinlong.modules.entity.Classification;
import com.liuxinlong.modules.entity.Standard;
import com.liuxinlong.modules.standard.service.StandardMapService;
import com.liuxinlong.utils.ObjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 标准地图服务层实现类
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022年8月30日
 */
@Slf4j
@Service
public class StandardMapServiceImpl implements StandardMapService {

    @Autowired
    private ClassificationDao classificationDao;

    @Autowired
    private StandardDao standardDao;

    @Override
    public TreeNode queryKnowledgeGraph(String rootId) {
        TreeNode root = new TreeNode();
        if (StringUtils.equals(rootId, "0")) {
            List<Classification> classificationList = classificationDao.getParentClassificationList();
            List<TreeNode> bodyList = new ArrayList<>();
            root.setId("0");
            root.setName("AOS");
            root.setValue("AOS");
            for (Classification classification : classificationList) {
                TreeNode treeNode = new TreeNode();
                treeNode.setId(classification.getId());
                treeNode.setName(classification.getName());
                treeNode.setValue(classification.getName());
                treeNode.setParentId("0");
                bodyList.add(treeNode);
            }
            root.setChildren(bodyList);
        } else {
            Classification classification = classificationDao.selectById(rootId);
            if (ObjectUtils.isEmpty(classification)) {
                throw new AosException(ReturnCodes.HTTP_PARAM_ERROR, "标准分类不存在！");
            }
            List<Standard> standardList = standardDao.getStandardListByClass(rootId, 0);
            List<TreeNode> bodyList = new ArrayList<>();
            root.setId(rootId);
            root.setName(classification.getName());
            root.setValue(classification.getName());
            root.setParentId("0");
            for (Standard standard : standardList) {
                TreeNode treeNode = new TreeNode();
                treeNode.setId(standard.getId());
                treeNode.setName(standard.getMasterPdfFile());
                treeNode.setValue(standard.getName());
                treeNode.setParentId(rootId);
                bodyList.add(treeNode);
            }
            root.setChildren(bodyList);
        }
        return root;
    }

    @Override
    public List<Map<String, Object>> queryMapDetail(String rootId, int startNum) {
        if (!StringUtils.isEmpty(rootId)) {
            Classification classification = classificationDao.selectById(rootId);
            if (ObjectUtils.isEmpty(classification)) {
                throw new AosException(ReturnCodes.HTTP_PARAM_ERROR, "标准分类不存在！");
            }
        }
        // 原始数据获取
        List<Standard> standardList = standardDao.getStandardListByClass(rootId, startNum);
//        int total = standardDao.getStandardCountByClass(rootId);
        Map<String, List<Standard>> originalMap = new HashMap<>();
        for (Standard item : standardList) {
            List<Standard> subClassList;
            String key = item.getClassification() + item.getSubClassification();
            if (originalMap.containsKey(key)) {
                subClassList = originalMap.get(key);
            } else {
                subClassList = new ArrayList<>();
            }
            subClassList.add(item);
            originalMap.put(key, subClassList);
        }

        List<Map<String, Object>> dataList = new ArrayList<>();
        // 组装数据
        for (List<Standard> list : originalMap.values()) {
            if (list.isEmpty()) {
                continue;
            }
            if (list.size() < 21) {
                Map<String, Object> dataMap = spliceData(list);
                dataList.add(dataMap);
                continue;
            }
            int totalPage = (int) Math.ceil((double) list.size() / 20);
            for (int i = 0; i < totalPage; i++) {
                List<Standard> subList = list.subList(20 * i, Math.min(20 * (i + 1),list.size()));
                Map<String, Object> dataMap = spliceData(subList);
                dataList.add(dataMap);
            }
        }

        return dataList;
    }

    /**
     * 拼接数据
     *
     * @param list 原始数据
     * @return 数据
     */
    private Map<String, Object> spliceData(List<Standard> list) {
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("layout", "odd-r");
        List<Map<String, Object>> bodyList = new ArrayList<>();
        int i = 0;
        int j = 0;
        for (Standard standard : list) {
            Map<String, Object> item = new HashMap<>();
            item.put("sn", standard.getStandardSn());
            item.put("type", "state");
            item.put("name", standard.getName());
            item.put("file", standard.getMasterPdfFile());
            item.put("r", j);
            item.put("q", i);
            if (i > 3) {
                j++;
                i = 0;
            } else {
                i++;
            }
            bodyList.add(item);
        }
        int num = bodyList.size();
        if (num < 20) {
            for (int s = 0; s < 20 - num; s++) {
                Map<String, Object> item = new HashMap<>();
                item.put("r", j);
                item.put("q", i);
                if (i > 3) {
                    j++;
                    i = 0;
                } else {
                    i++;
                }
                bodyList.add(item);
            }
        }
        dataMap.put("hexes", bodyList);
        return dataMap;
    }

//    @Override
//    public Map<String, Object> queryMapDetail(String rootId,int startNum) {
//        if (!StringUtils.isEmpty(rootId)) {
//            Classification classification = classificationDao.selectById(rootId);
//            if (ObjectUtils.isEmpty(classification)) {
//                throw new AosException(ReturnCodes.HTTP_PARAM_ERROR,"标准分类不存在！");
//            }
//        }
//        List<Standard> standardList = standardDao.getStandardListByClass(rootId,startNum);
//        int total = standardDao.getStandardCountByClass(rootId);
//        int totalPage = 0;
//        List<Map<String,Object>> bodyList = new ArrayList<>();
//        int i = 0;
//        int j = 0;
//        for (Standard standard : standardList) {
//            Map<String,Object> item = new HashMap<>();
//            item.put("sn",standard.getStandardSn());
//            item.put("type","state");
//            item.put("name",standard.getName());
//            item.put("file",standard.getMasterPdfFile());
//            item.put("r",j);
//            item.put("q",i);
//            if (i > 3) {
//                j ++;
//                i = 0;
//            } else {
//                i++;
//            }
//            bodyList.add(item);
//        }
//        int num = bodyList.size();
//        if (num < 20) {
//            for (int s = 0; s < 20-num;s ++) {
//                Map<String,Object> item = new HashMap<>();
//                item.put("r",j);
//                item.put("q",i);
//                if (i > 3) {
//                    j ++;
//                    i = 0;
//                } else {
//                    i++;
//                }
//                bodyList.add(item);
//            }
//        }
//
//        Map<String,Object> resultMap = new HashMap<>();
//        resultMap.put("layout","odd-r");
//        resultMap.put("hexes",bodyList);
//        resultMap.put("total",total);
//        resultMap.put("firstPage",startNum == 0);
//        resultMap.put("lastPage",num < 20);
//        resultMap.put("totalPage",totalPage);
//        return resultMap;
//    }

}
